﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using RentalIService;
using System.Web.Mvc;
using System.Net.Mail;
using System.Net;
using System.Security.Cryptography.X509Certificates;
using System.Net.Security;
using Common;
using ViewModel;

namespace RentalManagement.Controllers
{
    public class LoginController : Controller
    {

        private readonly IServiceStaff staff;
        private readonly IServiceRole role;
        private readonly IServicelandlordTenant lord;
        public LoginController(IServiceStaff _staff, IServicelandlordTenant _lord, IServiceRole _role)
        {
            staff = _staff;
            lord = _lord;
            role = _role;
        }
        // GET: Login
        public ActionResult Index(string msg)
        {
            ViewBag.msg = msg;
            return View();
        }
        public ActionResult Log()
        {
            string id = Request["id"].ToString();
            string pwd = Request["pwd"].ToString();

            var staf = staff.SelectOneList(id, pwd);
            var loa = lord.SelectOneList(id, pwd);
            LoginUser log = new LoginUser();
            if ((staf.Count == 1 && loa.Count == 0) || (staf.Count == 0 && loa.Count == 1))
            {
                if (staf.Count == 1)
                {
                    log.Id = staf.FirstOrDefault().Id;
                    log.Name = staf.FirstOrDefault().Name;
                    log.RoleId = staf.FirstOrDefault().RoleId;
                    log.RoleName = role.SelectRoleID(staf.FirstOrDefault().RoleId).FirstOrDefault().Name;
                }
                else
                {
                    log.Id = loa.FirstOrDefault().Id;
                    log.Name = loa.FirstOrDefault().Name;
                    log.RoleId = loa.FirstOrDefault().RoleId;
                    log.RoleName = role.SelectRoleID(loa.FirstOrDefault().RoleId).FirstOrDefault().Name;
                }

                //加密
                CookieHelper.SaveUserData(log);

                return Redirect("/Main/index");
            }
            else
            {
                return RedirectToAction("index","Login",new { msg="用户名或密码错误"});
            }

        }
        /// <summary>
        /// 忘记密码
        /// </summary>
        /// <returns></returns>
        public ActionResult Forget()
        {
            return View();
        }
        public ActionResult zhu()
        {
            ViewBag.role = role.SelectRoleID("F");
            return View();
        }
        public ActionResult Ti(string email, string id)
        {
            Random ro = new Random();
            var list = staff.SelectOneList(id);
            if (list.Count == 1)
            {
                MailAddress MessageFrom = new MailAddress("yang.le.192@qq.com"); //发件人邮箱地址
                string MessageTo = email; //收件人邮箱地址
                string MessageSubject = "密码找回"; //邮件主题
                string MessageBody = DateTime.Now.ToString("yMdHms"); //邮件内容 

                SendMail(MessageFrom, MessageTo, MessageSubject, MessageBody);
            }
            return Json(list.Count, JsonRequestBehavior.AllowGet);
        }
        /// <summary>
        /// 发送邮件
        /// </summary>
        /// <param name="MessageFrom">邮件发转发地址</param>
        /// <param name="MessageTo">发送给谁</param>
        /// <param name="MessageSubject">标题</param>
        /// <param name="MessageBody">内容</param>
        /// <returns></returns>
        public bool SendMail(MailAddress MessageFrom, string MessageTo, string MessageSubject, string MessageBody)   //发送验证邮件
        {
            MailMessage message = new MailMessage();
            message.To.Add(MessageTo);
            message.From = MessageFrom;
            message.Subject = MessageSubject;
            message.SubjectEncoding = System.Text.Encoding.UTF8;
            message.Body = MessageBody;
            message.BodyEncoding = System.Text.Encoding.UTF8;
            message.IsBodyHtml = true; //是否为html格式
            message.Priority = MailPriority.High; //发送邮件的优先等级
            SmtpClient sc = new SmtpClient();
            sc.EnableSsl = true;//是否SSL加密
            sc.Host = "smtp.qq.com"; //指定发送邮件的服务器地址或IP
            sc.Port = 587; //指定发送邮件端口
            sc.Credentials = new System.Net.NetworkCredential("yang.le.192@qq.com", "itangxmdphnjdchd"); //指定登录服务器的用户名和密码(注意：这里的密码是开通上面的pop3/smtp服务提供给你的授权密码，不是你的qq密码)

            try
            {
                ServicePointManager.ServerCertificateValidationCallback =
delegate (Object obj, X509Certificate certificate, X509Chain chain, SslPolicyErrors errors) { return true; };
                sc.Send(message); //发送邮件
            }
            catch (Exception e)
            {
                Response.Write(e.Message);
                return false;
            }
            return true;

        }
    }
}